import cv2
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage.exposure import histogram
import numpy as np


coins = cv2.imread("../testData/images/edges/edge1.png")
coins = np.floor(rgb2gray(coins) * 255)
# hist, hist_centers = histogram(coins)
# nzhist = np.nonzero(hist)
# fig, axes = plt.subplots(1, 2, figsize=(8, 3))
# axes[0].imshow(coins, cmap=plt.cm.gray)
# axes[0].axis('off')
# axes[1].plot(hist_centers, hist, lw=2)
# axes[1].set_title('histogram of gray values')
# plt.show()
#
from skimage.filters import sobel
elevation_map = sobel(coins)
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(elevation_map, cmap=plt.cm.gray)
ax.set_title('elevation map')
ax.axis('off')
plt.show()
#
# markers = np.zeros_like(coins)
# markers[coins < 200] = 1
# markers[coins > 200] = 2
# fig, ax = plt.subplots(figsize=(4, 3))
# ax.imshow(markers, cmap=plt.cm.gray)
# ax.set_title('markers')
# ax.axis('off')
# plt.show()

from skimage import segmentation
segmentation_coins = segmentation.watershed(elevation_map)
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(segmentation_coins, cmap=plt.cm.gray)
ax.set_title('segmentation')
ax.axis('off')
plt.show()